import {
  fetchUserList,
  fetchUser,
  modifyUser,
  addUser,
  userStreets,
  fetchStreets
} from './service';

const Model = {
  namespace: 'usersystemdandtable',
  state: {
    tableListDataSource: [],
    totalPageSize: 1,
  },
  effects: {
    // 获取企业信息表格 默认1页 10条
    *fetch({ payload }, { call, put }) {
      // const searchMap = {
      //   manage_name: "33"
      //   user_name: undefined
      //   manage_street: undefined
      // }
        const response = yield call(fetchUserList, payload)
        // Page: {
        //   PageNo: 1
        //   PageSize: 10
        //   TotalPage: 1
        //   TotalCount: 2
        //   FirstPage: true
        //   LastPage: true
        // }
        // List: [
        //   {
        //     "Streets": [
        //       {
        //         "Id": 1,
        //         "Name": "新安街道",
        //         "Order": 1
        //       }
        //     ],
        //     "Id": 2,
            // "Name": "管理员",
            // "Accout": "yifei",
            // "Dacl": 0,
            // "Password": "123457",
            // "IsRoot": 0
        //   }
        // ]
        // 
        response.List = response.List ? response.List : []
        const tableListDataSource = []
        response.List.map(item => {
          if (!item.IsRoot) {
            tableListDataSource.push({
              id: item.Id,
              user_name: item.Accout,
              manage_name: item.Name,
              manage_street: item.Streets.length > 0 ? item.Streets[0].Name : '',
              status: item.Dacl,
            })
          }
        })

        const res = {
          status: 200,
          totalPageSize: response.Page.TotalCount,
          tableListDataSource
        }

        yield put({
            type: 'save',
            payload: res,
        });

        return res
    },

    *fetchStreets(_, { call }) {
      const response = yield call(fetchStreets)
      const res = {
        status: 200,
        data: response
      }
      return res

    },

    *modifyStatus({ payload }, { call, put }) {
      // 获取原始数据
      const response = yield call(fetchUser, payload)
      console.log(response)
      // 修改数据
      response[0].Dacl = response[0].Dacl ? 0 : 1
      const modifyAfter = {
        id: response[0].Id,
        data: { ...response[0] }
      }
      // PUT修改后的数据
      const res = yield call(modifyUser, modifyAfter)
      if (res) {
        return 200
      }
      return 200
    },

    *modifyPwd({ payload }, { call, put }) {
      // 获取原始数据
      const response = yield call(fetchUser, payload)
      // 修改数据
      response[0].Password = payload.Pwd
      const modifyAfter = {
        id: response[0].Id,
        data: { ...response[0] }
      }
      // PUT修改后的数据
      const res = yield call(modifyUser, modifyAfter)
      if (res) {
        return 200
      }
      return 200
    },

    // 新增管理员
    *addUser({ payload }, { call, put }) {

      const newUser = {
        Name: payload.manage_name,
        Accout: payload.user_name,
        Dacl: Number(payload.status),
        Password: '123456789',
        IsRoot: 0,
      }

      const res = yield call(addUser, newUser)
      // 新增Streets
      const putUserStreets = {
        ...res,
        Streets: [
          {
            Id: Number(payload.manage_street),
          }
        ]
      }

      const resUserStreets = yield call(userStreets, putUserStreets)
      if (resUserStreets === 'OK') {
        return 200
      }
      return 200
    },













  },
  reducers: {
    save(state, { payload }) {
        return { ...state, tableListDataSource: payload.tableListDataSource, totalPageSize: payload.totalPageSize };
    },
  },
};
export default Model;
